package com.jyxd.web.controller.dictionary;

import com.jyxd.web.data.dictionary.NormalValueDictionary;
import com.jyxd.web.service.dictionary.NormalValueDictionaryService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "生命体征正常值字典相关接口")
@Controller
@RequestMapping(value = "/NormalValueDictionary")
public class NormalValueDictionaryController {

    private static Logger logger= LoggerFactory.getLogger(NormalValueDictionaryController.class);

    @Autowired
    private NormalValueDictionaryService normalValueDictionaryService;

    /**
     * 增加一条生命体征正常值字典记录
     * @return
     */
    @ApiOperation("增加一条生命体征正常值字典记录")
    @RequestMapping(value = "/insert",method = {RequestMethod.POST})
    @ResponseBody
    public String insert(@RequestBody NormalValueDictionary normalValueDictionary){
        JSONObject json=new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg", "系统开小差了，请稍后再试。");
        Map<String,Object> map=new HashMap<>();
        map.put("itemCode",normalValueDictionary.getItemCode());
        List<NormalValueDictionary> normalValueDictionaries = normalValueDictionaryService.queryList(map);
        if(normalValueDictionaries.size()>0){
            json.put("code",HttpCode.EXISTING_CODE.getCode());
            json.put("msg","该生命体征正常值已存在，请重新输入");
        }else{
            normalValueDictionary.setId(UUIDUtil.getUUID());
            normalValueDictionaryService.insert(normalValueDictionary);
            json.put("code",HttpCode.OK_CODE.getCode());
            json.put("msg","添加成功");
        }
        return json.toString();
    }

    /**
     * 更新生命体征正常值状态
     * @param map
     * @return
     */
    @ApiOperation("更新生命体征正常值状态")
    @RequestMapping(value = "/update",method = {RequestMethod.POST})
    @ResponseBody
    public String update(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("msg","系统开小差了，请稍后再试。");
        if(map!=null && map.containsKey("id") && map.containsKey("status") ){
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("id",map.get("id").toString());
            List<NormalValueDictionary> normalValueDictionaries = normalValueDictionaryService.queryList(queryMap);
            if(normalValueDictionaries.size()>0){
                NormalValueDictionary normalValueDictionary = normalValueDictionaries.get(0);
                normalValueDictionary.setStatus((int)map.get("status"));
                normalValueDictionaryService.update(normalValueDictionary);
                json.put("msg","更新成功");
            }else{
                json.put("msg","更新失败，没有这个对象。");
                return json.toString();
            }
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 编辑床位地点记录
     * @param map
     * @return
     */
    @ApiOperation("编辑生命体征正常值")
    @RequestMapping(value = "/edit",method = {RequestMethod.POST})
    @ResponseBody
    public String edit(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("msg","系统开小差了，请稍后再试。");
        if(map!=null && map.containsKey("id")){
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("id",map.get("id").toString());
            List<NormalValueDictionary> normalValueDictionaries = normalValueDictionaryService.queryList(queryMap);
            if(normalValueDictionaries.size()>0){
                NormalValueDictionary normalValueDictionary = normalValueDictionaries.get(0);
                if (map.containsKey("maxValue")){
                    normalValueDictionary.setMaxValue((String) map.get("maxValue"));
                }
                if (map.containsKey("minimumValue")){
                    normalValueDictionary.setMinimumValue((String) map.get("minimumValue"));
                }
                if (map.containsKey("status")){
                    normalValueDictionary.setStatus((Integer) map.get("status"));
                }
                normalValueDictionaryService.update(normalValueDictionary);
                json.put("msg","编辑成功");
            }else{
                json.put("msg","编辑失败，没有这个对象。");
                return json.toString();
            }
        }
        json.put("code",HttpCode.OK_CODE.getCode());

        return json.toString();
    }

    /**
     * 根据条件分页查询床位字典记录列表（也可以不分页）
     * @param map
     * @return
     */
    @ApiOperation("根据条件分页查询床位字典记录列表（也可以不分页）")
    @RequestMapping(value = "/queryList",method= RequestMethod.POST)
    @ResponseBody
    public String queryList(@RequestBody(required=false) Map<String,Object> map){
        JSONObject json=new JSONObject();
        json.put("code",HttpCode.FAILURE_CODE.getCode());
        json.put("data",new ArrayList<>());
        json.put("msg","暂无数据");
        if(map!=null && map.containsKey("start")){
            int totalCount =normalValueDictionaryService.queryNum(map);
            map.put("start",((int)map.get("start")-1)*(int)map.get("size"));
            json.put("totalCount",totalCount);
        }
        List<NormalValueDictionary> normalValueDictionaries = normalValueDictionaryService.queryList(map);
        if(normalValueDictionaries!=null && normalValueDictionaries.size()>0){
            json.put("data",JSONArray.fromObject(normalValueDictionaries));
            json.put("msg","查询成功");
        }
        json.put("code",HttpCode.OK_CODE.getCode());
        return json.toString();
    }


}
